package com.common.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.ResourceBundle;
import java.util.UUID;

/**
 * 项目参数工具类
 *
 * @author Stone
 */
public class ResourceUtil {
    private static final ResourceBundle bundle = ResourceBundle.getBundle("sysconfig");

    /**
     * 获取配置文件参数
     *
     * @param name
     * @return
     */
    public static final String getConfigByName(String name) {
        return bundle.getString(name);
    }

    /**
     * 获得请求路径
     *
     * @param request
     * @return
     */
    public static String getRequestPath(HttpServletRequest request) {
        String requestPath = request.getRequestURI() + "?" + request.getQueryString();
        //去掉其他参数
        if (requestPath.indexOf("&") > -1) {
            requestPath = requestPath.substring(0, requestPath.indexOf("&"));
        }
        //去掉项目路径
        requestPath = requestPath.substring(request.getContextPath().length() + 1);
        return requestPath;
    }

    /**
     * 获取系统路径
     *
     * @return
     */
    public static String getSysPath() {
        String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
        //String temp = path.replaceFirst("file:/", "").replaceFirst("WEB-INF/classes/", "");
        //适用window和linux系统
        String temp = path.replaceFirst("file:", "").replaceFirst("WEB-INF/classes/", "");
        String separator = System.getProperty("file.separator");
        String resultPath = temp.replaceAll("/", separator + separator).replaceAll("%20", " ");

        return resultPath;
    }

    /**
     * 获取class文件路径
     *
     * @return
     */
    public static String getClassPath() {
        String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
        //String temp = path.replaceFirst("file:/", "");
        //适用window和linux系统
        String temp = path.replaceFirst("file:", "");
        String separator = System.getProperty("file.separator");
        String resultPath = temp.replaceAll("/", separator + separator);
        return resultPath;
    }

    /**
     * 获取项目根目录
     *
     * @return
     */
    public static String getPorjectPath() {
        String nowpath; //当前tomcat的bin目录的路径 如 D:\java\software\apache-tomcat-6.0.14\bin
        String tempdir;
        nowpath = System.getProperty("user.dir");
        tempdir = nowpath.replace("bin", "webapps"); //把bin 文件夹变到 webapps文件里面
        tempdir += "\\"; //拼成D:\java\software\apache-tomcat-6.0.14\webapps\sz_pro
        return tempdir;
    }

    /**
     * 获取32位UUID
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
}
